package com.itheima.reggie.controller;

import com.itheima.reggie.config.R;
import com.itheima.reggie.dao.SetmealDao;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.SetmealDishVo;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    /***
     * 保存套餐
     */
    @PostMapping
    public R saveSctmeal(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealDto.setCreateUser(empId);
        setmealService.save(setmealDto);
        return R.success("保存成功");

    }

    /**
     * f分也查询
     * http://localhost:8080/setmeal/page?page=1&pageSize=10
     * get
     */
    @GetMapping("/page")
    public R pageList(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize, String name) {

        Page<SetmealDto> setmealDtoList = setmealService.getList(page, pageSize, name);
        return R.success(setmealDtoList);

    }
    /*
    http://localhost:8080/backend/index.html
    删除
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        setmealService.delete(ids);
        return R.success("删除成功");

    } /*
   localhost:8080/setmeal/list?categoryId=1413342269393674242&status=1
        套餐
     */
    @GetMapping("/list")
    public R list(Long categoryId,Integer status) {
        List<SetmealDto>setmealDtoList= setmealService.list(categoryId,status);
        return R.success(setmealDtoList);

    }
    /**
     * http://localhost:8080/setmeal/dish/1415580119015145481
     * get
     */
    @GetMapping("/dish/{id}")
    public R setmealIdlist(@PathVariable("id") Long setmealId) {
        List<SetmealDishVo>SetmealDishVoList= setmealService.findAll(setmealId);
        return R.success(SetmealDishVoList);

    }
}
